package com.ts.api.module.match.plan.model;

import com.ts.api.common.constant.product.ProductPlanTypeEnum;
import com.ts.api.module.api.entity.bo.ApiBizMatchBO;
import com.ts.api.module.api.entity.bo.ApiParameterBO;
import com.ts.api.module.api.entity.po.ApiKeyPO;
import com.ts.api.module.api.entity.vo.ReqApiParameterVO;
import com.ts.api.module.match.plan.internal.core.PlanMatchChannelType;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

/**
 *  API渠道匹配实体
 *
 *  @author Pan
 */
@Data
@EqualsAndHashCode(callSuper = true)
public class ApiPlanMatch extends AbstractPlanMatch {

    /** API客户端 */
    private ApiKeyPO apiKeyPO;
    /** API传输请求实体 */
    private ReqApiParameterVO reqApiParameterVO;

    public ApiPlanMatch() {
        super();
    }

    /**
     *  开启允许多推
     *
     *  @author Pan
     */
    public void enableAllowMultiPush() {
        getPlanMatchConfig().setAllowMultiPush(true);
    }

    /**
     *  关闭允许多推
     *
     *  @author Pan
     */
    public void disableAllowMultiPush() {
        getPlanMatchConfig().setAllowMultiPush(false);
    }

    /**
     *  构建匹配实体
     *
     *  @param  apiParameterBO   API传递参数业务实体
     *  @return ApiPlanMatch
     */
    public static ApiPlanMatch ofMatch(ApiParameterBO apiParameterBO) {
        ApiKeyPO apiKeyPO = apiParameterBO.getApiKeyPO();
        ReqApiParameterVO reqApiParameterVO = apiParameterBO.getReqApiParameterVO();
        ApiBizMatchBO apiBizMatchBO = apiParameterBO.getApiBizMatchBO();
        List<ProductPlanTypeEnum> listPlanType = apiBizMatchBO.getListPlanType();

        ApiPlanMatch apiPlanMatch = new ApiPlanMatch();
        apiPlanMatch.setLoanChannelId(apiKeyPO.getLoanChannelId());
        apiPlanMatch.setApiKeyPO(apiKeyPO);
        apiPlanMatch.setReqApiParameterVO(reqApiParameterVO);
        apiPlanMatch.setListPlanType(listPlanType);
        apiPlanMatch.setPlanMatchChannelType(PlanMatchChannelType.API);
        apiPlanMatch.setHttpTimeout(apiKeyPO.getApiTimeoutLimit());
        apiPlanMatch.setChannelPrice(apiKeyPO.getApiPrice());
        apiPlanMatch.setChannelShareRatio(apiKeyPO.getApiShare());
        return apiPlanMatch;
    }
}
